********Figure 4********

*Specification chart program

clear
cap program drop specchart
program specchart
syntax varlist, [replace] [sizerep] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	*sizerep is for when you want this for sizerep
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			save "estimates.dta",replace
	}	
	else{
		* load dataset
		use "estimates.dta",clear
	}
	
	if "`sizerep'"=="" {
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
	}
	else {
		* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta = _b[`varlist'] if  spec_id==`obs'
	replace se=e(N)   if  spec_id==`obs'
	*replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	*replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	*replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	*replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
	
	}
	
		save "estimates.dta",replace
	* restore dataset
	use `temp',clear
end


*Load data
cd "$reploc/data/created_data"
use indo_full, clear

sort pidlink ifls
drop if ifls==.
keep if panel==1

keep rbuc*  gambleaverse ifls pidlink inflation imp_age panel biryear_impl grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co? grexprovSD_lam0co? birprov_impl evermig

egen evermig1=total(evermig), by(pidlink)

reshape wide rbuc*  gambleaverse inflation imp_age grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co? grexprovSD_lam0co? evermig, i(pidlink) j(ifls)


local varlist1 rbuc rbucB rbuc_nga rbuc_garefined rbucgaref1a rbucgaref2a rbucgaref3a rbucgaref4a rbuc_garefineddbGA gambleaverse inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co2 grexprovSD_lam0co2 grexprovAvg_lam0co3 grexprovSD_lam0co3 


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'5-`var'4
}

*Generate cohort by birthyear variables to cluster on
egen coh_prov_clust=group(biryear_impl birprov_impl)

tempfile savi
save "`savi'", replace

*Results

local varint Avg SD sizeAvg sizeSD

foreach varinti in `varint' {
use "`savi'", clear

*Main specification
reg rbuc_garefineddif  grbrprovAvg_lam0co3dif grbrprovSD_lam0co3dif  inflationdif, vce(cluster coh_prov_clust)   

if "`varinti'"=="Avg" | "`varinti'"=="SD"{ 
specchart grbrprov`varinti'_lam0co3dif,spec(main birthprov yesmigs rbuc_garefined oldindoonly) replace
}

if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
local hola=substr("`varinti'",5,.)
specchart grbrprov`hola'_lam0co3dif,spec(main birthprov yesmigs rbuc_garefined oldindoonly) replace sizerep
}


*Loop over all alternative specifications
local locs birthprov lifeprov
local rbucspecs rbuc rbucgaref1a rbuc_garefined rbuc_garefineddbGA rbuc_nga
local agespecs oldlike77 oldindotot oldindoonly post77
local migs yesmigs nomigs

 
foreach loci in `locs' {

if "`loci'"=="lifeprov" {
local usei grex
}


if "`loci'"=="birthprov" {
local usei grbr
}

foreach migi in `migs' {


if "`migi'"=="nomigs" {
drop if evermig1>0 
}

foreach rbucspeci in `rbucspecs' {

foreach agespeci in `agespecs' {

preserve


if "`agespeci'"=="oldlike77" {
local rhsvaruse
}

if "`agespeci'"=="oldindotot" {
local rhsvaruse co2
}

if "`agespeci'"=="oldindoonly" {
local rhsvaruse co3
}

if "`agespeci'"=="post77" {
keep if biryear_impl>=1977
local rhsvaruse
}


qui: reg `rbucspeci'dif  `usei'provAvg_lam0`rhsvaruse'dif `usei'provSD_lam0`rhsvaruse'dif  inflationdif, vce(cluster coh_prov_clust)    

if "`varinti'"=="Avg" | "`varinti'"=="SD"{ 
specchart `usei'prov`varinti'_lam0`rhsvaruse'dif,spec(alter `loci' `migi' `rbucspeci' `agespeci')
}

if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
local hola=substr("`varinti'",5,.)
specchart `usei'prov`hola'_lam0`rhsvaruse'dif,spec(alter `loci' `migi' `rbucspeci' `agespeci') sizerep
}

restore

}
}
}
}

*Create chart

use "estimates.dta",clear

*Drop duplicate of main
duplicates drop  oldlike77 oldindotot oldindoonly post77 rbuc rbucgaref1a rbuc_garefined rbuc_garefineddbGA rbuc_nga yesmigs nomigs birthprov lifeprov, force

gen locicat=1 if birthprov==1
replace locicat=2 if lifeprov==1

gen migicat=1 if yesmigs==1
replace migicat=2 if nomigs==2

gen rbuccat=1 if rbuc_garefined==1
replace rbuccat=2 if rbucgaref1a==1
replace rbuccat=3 if rbuc_nga==1
replace rbuccat=4 if rbuc==1

gen oldcat=1 if oldindoonly==1
replace oldcat=2 if post77==1 
replace oldcat=3 if oldlike77==1

sort beta
gen rank=_n

	local scoff=" "
	local scon=" "
	
if "`varinti'"=="Avg" {
	local ind=-1.65
	}
	
	if "`varinti'"=="SD" {
	local ind=-0.5
	}
	
		if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
	local ind=-1000
	}
	
	foreach var in oldlike77 oldindotot oldindoonly post77 rbuc rbucgaref1a rbuc_garefined rbuc_garefineddbGA rbuc_nga yesmigs nomigs birthprov lifeprov {
	
	
	   cap gen i_`var'=`ind'
	   
	 *Leave space for each new category to put names
	   
	   if "`varinti'"=="Avg" | "`varinti'"=="SD" {
	   if  "`var'"=="post77" |  "`var'"=="rbuc_nga" | "`var'"=="nomigs" {
	   local ind=`ind'-0.3
	   }
	   
	   else {
	   local ind=`ind'-0.15
	   }
	   }
	   
	   
	   else {
	   	   if  "`var'"=="post77" |  "`var'"=="rbuc_nga" | "`var'"=="nomigs" {
	   local ind=`ind'-2000
	   }
	   
	   else {
	   local ind=`ind'-1000
	   }
	   }
	  
	  local scoff="`scoff' (scatter i_`var' rank if `var'==., msymbol(circle_hollow) msize(vsmall) mcolor(gs10))" 
	  local scon="`scon' (scatter i_`var' rank if `var'==1, msymbol(X) msize(small) mcolor(black))" 
      local scon="`scon' (scatter i_`var' rank if `var'==1 & main==1, msymbol(X) msize(small) mcolor(orange))" 
	}



*Plot

if "`varinti'"=="Avg" {

tw  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) barwidth(0.1) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) barwidth(0.1) lwidth(none))  /// 90% CI
   (scatter beta rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
   `scoff' `scon' /// indicators for spec
  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
,legend (order(1 "Preferred spec." 4 "Point estimate" 2 "95% CI" 3 "90% CI") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") ///
   yscale(noline) xscale(noline) ylab(-1.25(0.5)0.25, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) yline(0, lcolor(red))
   
   
gr_edit .yaxis1.add_ticks -1.5 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -1.65 `"Treat all Old as born in 1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.8 `"Born 1961-1976 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.95 `"Age 0-15 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.1 `"Drop Cohorts Born pre-1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.25 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.4 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.55 `"Drop GA w/ Task B Inconsist."', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -2.7 `"Drop GA w/ Task B Inconsist. + Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.85 `"Drop GA w/ Task B Inconsist. + Observables + GAx2"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3.15 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -3.3 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -3.45 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -3.6 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -3.75 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3.9 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks 0.5 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   
   
   if "`varinti'"=="SD" {

tw  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) barwidth(0.1) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) barwidth(0.1) lwidth(none))  /// 90% CI
   (scatter beta rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
   `scoff' `scon' /// indicators for spec
  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
,legend (order(1 "Preferred spec." 4 "Point estimate" 2 "95% CI" 3 "90% CI") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(vsmall)) ///
   xtitle(" ") ytitle(" ") ///
   yscale(noline) xscale(noline) ylab(0(0.25)1.5, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) yline(0, lcolor(red))
   
   
   
gr_edit .yaxis1.add_ticks -0.35 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -0.5 `"Treat all Old as born in 1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.65 `"Born 1961-1976 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.8 `"Age 0-15 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.95 `"Drop Cohorts Born pre-1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.1 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -1.25 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.4 `"Drop GA w/ Task B Inconsist."', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -1.55 `"Drop GA w/ Task B Inconsist. + Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.7 `"Drop GA w/ Task B Inconsist. + Observables + GAx2"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.85 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.15 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -2.3 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -2.45 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.6 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.75 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )


gr_edit .yaxis1.add_ticks 1.75 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   
   
   if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {

tw  (scatter se rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (scatter se rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
  (scatter se rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   `scoff' `scon' /// indicators for spec add to bove if want
,legend (order(1 "Preferred spec.") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") ///
    yscale(noline) xscale(noline) ylab(0(0.25)1.5, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   ylab(2000(2000)18000, angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white))
   
   



gr_edit .yaxis1.add_ticks 0 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -1000 `"Treat all Old as born in 1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2000 `"Born 1961-1976 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3000 `"Age 0-15 Country Level Growth (Born 1961+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -4000 `"Drop Cohorts Born pre-1977"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -5000 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -6000 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -7000 `"Drop GA w/ Task B Inconsist."', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -8000 `"Drop GA w/ Task B Inconsist. + Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -9000 `"Drop GA w/ Task B Inconsist. + Observables + GAx2"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -10000 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -11000 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -12000 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -13000 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -14000 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -15000 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -16000 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
*/


gr_edit .yaxis1.add_ticks 20000 `"Sample Size"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   

cd "$reploc/results"
graph export Figure4`varinti'.eps, replace

graph close

}

